from collections import Iterable
import time

""" 要实现迭代器
1.判断这个类是否属于Iterable类
2.这个类必须有__iter__方法
3.__iter__方法必须返回一个具有__next__方法的类
4.__next__方法必须有一个raise StopIteration
 """


class Classmate(object):
    def __init__(self):
        self.names = list()
        self.current_num = 0

    def add(self, name):
        self.names.append(name)

    def __iter__(self):
        return self

    def __next__(self):
        if self.current_num < len(self.names):
            ret = self.names[self.current_num]
            self.current_num += 1
            return ret
        else:
            raise StopIteration


classmate = Classmate()
classmate.add('11')
classmate.add('22')
classmate.add('33')
# print(isinstance('结果为', classmate, Iterable))

for i in classmate:
    print(i)
    time.sleep(1)
